package dfsketches.gr3d;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Scene {

//	private Hashtable<String, Camera> cameras;
	protected HashMap<String, Model> models;
	protected Camera activeCamera;
	protected List<Instance> instances;
	
	public Scene() {
		models = new HashMap<String, Model>();
		activeCamera = new Camera(1000f, 1000f);
		instances = new ArrayList<Instance>();
	}

	public void setActiveCamera(Camera cam) {
		activeCamera = cam;
	}
	
	public void apply(Matrix3D m) {
		for (Instance ins : instances) {
			ins.apply(m);
		}
	}
	
	public void render(Renderer r, Cutter c, Object m) {
		synchronized (m) {
			r.start();
			for (Instance ins : instances) {
				ins.render(activeCamera, c, r);
			}
		}
	}

	public void reanimate() {
		activeCamera.reanimate();
		for (Instance ins : instances) {
			ins.reanimation(activeCamera);
		}
	}

	public long getClickedObject(int x, int y) {
		float minDist = Float.MAX_VALUE;
		long number = -1;
		for (Instance ins : instances) {
			float dist = ins.isClicked(x, y, activeCamera);
			if (minDist > dist && dist > 0f) {
				minDist = dist;
				number = ins.getInstanceNumber();
			}
		}
		return number;
	}
	
	public Instance addInstanceOfModel(String modelName) {
		Instance inst = new Instance(models.get(modelName));
		instances.add(inst);
		return inst;
	}
	
	public void addModel(Model m, String name) {
		models.put(name, m);
	}

	public Model getModel(String name) {
		return models.get(name);
	}
	
}